﻿using System;
using System.Collections.Generic;
using System.IO;
using BCL.BaseLib.Basics;
namespace DevicesView.Logic.App
{
    [Serializable]
    public class UserConfig
    {
        private static readonly string ConfigPath;
        private static string userCFPath;
        private string id;
        static UserConfig()
        {
            ConfigPath = AppDomain.CurrentDomain.BaseDirectory + "UserConfig\\";
            if (!Directory.Exists(ConfigPath))
                Directory.CreateDirectory(ConfigPath);
        }

        public UserConfig(int userId)
        {
            userCFPath = ConfigPath + userId + ".xml";
        }

        public string UserId
        {
            set
            {
                userCFPath = ConfigPath + value + ".xml";
                id = value;
            }
            get { return id; }
        }

        public UserConfig()
        {

        }
        /// <summary>
        /// 主页
        /// </summary>
        public string IndexAppId { get; set; }
        /// <summary>
        /// 用户应用
        /// </summary>
        public List<string> MyApps { get; set; }

        /// <summary>
        /// 当前登录的机构
        /// </summary>
        public string LoginOrgId { get; set; }

        /// <summary>
        /// 自动登录
        /// </summary>
        public bool AutoLogin { get; set; }
        /// <summary>
        /// 记住密码
        /// </summary>
        public bool Remember { get; set; }

        /// <summary>
        /// 登录名
        /// </summary>
        public string LoginName { set; get; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { set; get; }


        //public all
        public static UserConfig GetConfig(int userId)
        {
            UserConfig app;
            try
            {
                if (File.Exists(ConfigPath + userId + ".xml"))
                    return Tool.GetObjectByXML<UserConfig>(ConfigPath + userId + ".xml");
                else
                    app = new UserConfig(userId);
            }
            catch (Exception ex)
            {
                app = new UserConfig(userId);
                BCLApplication.log?.Error(ex);
            }
            return app;
        }
        public void Save()
        {
            this.LoginName = BCL.Tool.DES.EncryptDES(LoginName);
            if (Remember)
                this.Password = BCL.Tool.DES.EncryptDES(Password);
            Tool.ObjectSaveToXML(this, userCFPath);
        }

        public static List<UserConfig> GetLoginUserNames()
        {

            string[] files = Directory.GetFiles(ConfigPath, "*.xml");
            if (files == null || files.Length == 0)
                return null;

            List<UserConfig> usernames = new List<UserConfig>();
            foreach (string item in files)
            {
                try
                {
                    string id = item.Substring(item.LastIndexOf("\\") + 1).Replace(".xml", "");
                    UserConfig uc = GetConfig(int.Parse(id));

                    if (uc != null)
                    {
                        if (!string.IsNullOrEmpty(uc.LoginName))
                            uc.LoginName = BCL.Tool.DES.DecryptDES(uc.LoginName);
                        if(uc.Remember && !string.IsNullOrEmpty(uc.Password))
                            uc.Password= BCL.Tool.DES.DecryptDES(uc.Password);
                        usernames.Add(uc);
                    }

                }
                catch (Exception ex)
                {
                    BCLApplication.log?.Error(ex);
                    // HIHS.BaseLib.Basics.HIHSApplication.log.Error(ex);
                }
            }

            return usernames;
        }
    }
}
